package com.cd.dao;

import com.cd.entity.MovieCategory;
import com.cd.entity.MovieCategoryExample;
import com.cd.mapper.MovieCategoryMapper;
import com.cd.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.util.List;

/**
 * 类名：MovieCategoryDao
 * 作者：WF
 * 功能：电影分类数据访问对象
 */
public class MovieCategoryDao {

    // 1. 查询所有电影分类
    public List<MovieCategory> findAll() throws IOException {
        try (SqlSession session = MybatisUtil.getSqlSession(true)) {
            MovieCategoryMapper mapper = session.getMapper(MovieCategoryMapper.class);
            return mapper.selectByExample(null);
        }
    }

    // 2. 添加电影分类
    public void add(MovieCategory category) throws IOException {
        try (SqlSession session = MybatisUtil.getSqlSession(true)) {
            MovieCategoryMapper mapper = session.getMapper(MovieCategoryMapper.class);
            mapper.insert(category);
        }
    }

    // 3. 修改电影分类
    public void update(MovieCategory category) throws IOException {
        try (SqlSession session = MybatisUtil.getSqlSession(true)) {
            MovieCategoryMapper mapper = session.getMapper(MovieCategoryMapper.class);
            mapper.updateByPrimaryKeySelective(category);
        }
    }

    // 4. 删除电影分类
    public void delete(Integer categoryId) throws IOException {
        try (SqlSession session = MybatisUtil.getSqlSession(true)) {
            MovieCategoryMapper mapper = session.getMapper(MovieCategoryMapper.class);
            mapper.deleteByPrimaryKey(categoryId);
        }
    }

    // 5. 根据条件查询电影分类
    public List<MovieCategory> search(MovieCategoryExample example) throws IOException {
        try (SqlSession session = MybatisUtil.getSqlSession(true)) {
            MovieCategoryMapper mapper = session.getMapper(MovieCategoryMapper.class);
            return mapper.selectByExample(example);
        }
    }
}